package cn.tedu.rocket;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Slf4j
public class MyConsumer {

    @Test
    public void consume() throws Exception {
        //1. 消费者对象,连接namesrv 设置消费者分组,设置订阅的主题和消息过滤的条件
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer();
        // 设置nameserver地址
        consumer.setNamesrvAddr("localhost:9876");
        // 设置消费者组名
        consumer.setConsumerGroup("c-group01");
        // 订阅主题
        consumer.subscribe("topic01","*");
        //2. 创建绑定一个消费的监听器 随时监听 rocketmq是否push过来新的消息 消费逻辑就是在监听器实现的
        MessageListenerConcurrently messageListener = new MessageListenerConcurrently() {
            /**
             * 底层rocketmq客户端消费的核心方法,每个消息都会调用一次这个方法
             * @param list 消息对象 (push下,list元素只有一个)
             * @param consumeConcurrentlyContext 消息对象身份信息
             * @return 消费状态 (成功 CONSUME_SUCCESS/ 等待下一次消费 RECONSUME_LATER )
             */
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                MessageExt messageExt = list.get(0);
                byte[] body = messageExt.getBody();
                String msg = new String(body, StandardCharsets.UTF_8);
                System.out.println("消费者接收到消息:"+msg);
                // 返回消费状态,表示消息被消费成功
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        };
        consumer.setMessageListener(messageListener);

        //3. 开启连接 监听逻辑是异步执行
        consumer.start();
        while(true){} //自造死循环,保持程序运行

    }

    @Test
    public void consume2() throws Exception {
        //1. 消费者对象,连接namesrv 设置消费者分组,设置订阅的主题和消息过滤的条件
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer();
        // 设置nameserver地址
        consumer.setNamesrvAddr("localhost:9876");
        // 设置消费者组名
        consumer.setConsumerGroup("c-group02");
        // 订阅主题
        consumer.subscribe("topic03","*");
        //2. 创建绑定一个消费的监听器 随时监听 rocketmq是否push过来新的消息 消费逻辑就是在监听器实现的
        MessageListenerConcurrently messageListener = new MessageListenerConcurrently() {
            /**
             * 底层rocketmq客户端消费的核心方法,每个消息都会调用一次这个方法
             * @param list 消息对象 (push下,list元素只有一个)
             * @param consumeConcurrentlyContext 消息对象身份信息
             * @return 消费状态 (成功 CONSUME_SUCCESS/ 等待下一次消费 RECONSUME_LATER )
             */
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                MessageExt messageExt = list.get(0);
                byte[] body = messageExt.getBody();
                String msg = new String(body, StandardCharsets.UTF_8);
                log.info("消费者接收到消息:"+msg);
                // 返回消费状态,表示消息被消费成功
                return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }
        };
        consumer.setMessageListener(messageListener);

        //3. 开启连接 监听逻辑是异步执行
        consumer.start();
        while(true){} //自造死循环,保持程序运行

    }

}










